#include <string.h>

#include "mcx.h"

xmlNodePtr mcx_validityInterval_offset(xmlDocPtr doc, xmlNsPtr *ns, int start, int end) {
  xmlNodePtr e;
  xmlNsPtr dosh = NULL;
  char *tmp;

  if(!ns)
    ns = &dosh;
  if(!*ns)
    *ns = xmlNewNs(xmlDocGetRootElement(doc), ns_mcx, "mcx");

  e = xmlNewDocNode(doc, *ns, "validityInterval", NULL);

  tmp = mcx_time(time(NULL) + start);
  tmp[strlen(tmp) - 6] = 'Z';
  tmp[strlen(tmp) - 5] = 0;
  xmlAddChild(e, xmlNewDocNode(doc, *ns, "notBefore", tmp));
  tmp = mcx_time(time(NULL) + end);
  tmp[strlen(tmp) - 6] = 'Z';
  tmp[strlen(tmp) - 5] = 0;
  xmlAddChild(e, xmlNewDocNode(doc, *ns, "notAfter", tmp));
  return e;
}

xmlDocPtr mcx_doc_new(const char *ns, const char *prefix, const char *root, xmlNsPtr *nsptr, xmlNodePtr *rptr) {
  xmlDocPtr doc;
  xmlNodePtr e;
  xmlNsPtr nsp;

  if(!nsptr)
    nsptr = &nsp;
  if(!rptr)
    rptr = &e;
  
  doc = xmlNewDoc("1.0");
  *rptr = xmlNewDocNode(doc, NULL, root, NULL);
  *nsptr = xmlNewNs(*rptr, ns, prefix);
  xmlSetNs(*rptr, *nsptr);
  xmlDocSetRootElement(doc, *rptr);
  return doc;
}
